package org.example.workbench.controller;

import org.example.commons.constant.Constant;
import org.example.commons.domain.ErrorInfo;
import org.example.commons.utils.UUIDUtils;
import org.example.workbench.domain.ContactsActivityRelation;
import org.example.workbench.domain.activity;
import org.example.workbench.service.ActivityService;
import org.example.workbench.service.ContactsActivityRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/contactsActivityRelation")
public class ContactsActivityRelationController {
    @Autowired
    private ContactsActivityRelationService contactsActivityRelationService;
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/deleteRelationByContactsIdAndActivityId")
    public @ResponseBody Object deleteRelationByContactsIdAndActivityId(@RequestParam Map<String,Object> map){
        ErrorInfo errorInfo = new ErrorInfo();
        try{
            int i = contactsActivityRelationService.deleteRelationByContactsIdAndActivityId(map);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后......");
            }
        }catch(Exception e){
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后......");
        }
        return errorInfo;
    }
    @RequestMapping("/insertManyByList")
    public @ResponseBody Object insertManyByList(String contactsId,String[] activityId){
        ArrayList<ContactsActivityRelation> contactsActivityRelationList = new ArrayList<>();
        for (String ad : activityId) {
            ContactsActivityRelation relation = new ContactsActivityRelation();
            relation.setId(UUIDUtils.getUUID());
            relation.setContactsId(contactsId);
            relation.setActivityId(ad);
            contactsActivityRelationList.add(relation);
        }
        ErrorInfo errorInfo = new ErrorInfo();
        List<activity> activityList = null;
        try{
            int i = contactsActivityRelationService.insertManyByList(contactsActivityRelationList);
            activityList = activityService.getActivityByContactsId(contactsId);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后......");
            }
        }catch(Exception e){
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后......");
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("activityList",activityList);
        hashMap.put("errorInfo",errorInfo);
        return hashMap;
    }
}
